package QianFeng16.XML;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;

public class PersonHandler extends DefaultHandler {

    //存储解析后的数据
    private List<Person> persons = null;
    //当前正在解析的person
    private Person p;
    //用于记录当前正在解析的标签名
    private String tag;

    public List<Person> getPersons() {
        return persons;
    }

    //开始解析文档时调用
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        persons = new ArrayList<>();
        System.out.println("开始解析文档");
    }

    //在XML文档解析结束时调用
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        System.out.println("解析文档结束");
    }

    /**
     * 解析开始元素时调用
     *
     * @param uri 命名空间
     * @param localName 不带前缀的标签名
     * @param qName 带前缀的标签名
     * @param attributes 当前标签的属性集合
     * @throws SAXException 抛出异常
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if ("person".equals(qName)) {
            p = new Person();
            String personid = attributes.getValue("personid");
            p.setPersonid(personid);
        }
        tag = qName;
        System.out.println("startElement--" + qName);
    }

    //解析结束元素时调用
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if ("person".equals(qName)) {
            persons.add(p);
        }
        tag = null;
        System.out.println("endElement--" + qName);
    }

    //解析文本内容时调用
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        if (tag != null) {
            switch (tag) {
                case "name":
                    p.setName(new String(ch, start, length));
                    break;
                case "address":
                    p.setAddress(new String(ch, start, length));
                    break;
                case "tel":
                    p.setTel(new String(ch, start, length));
                    break;
                case "fax":
                    p.setFax(new String(ch, start, length));
                    break;
                case "email":
                    p.setEmail(new String(ch, start, length));
                    break;
            }
            //System.out.println(ch);
        }
    }
}
